package com.probiz.estoresa.theme.service.impl;

import com.probiz.estore.common.helper.ConfigUtil;
import com.probiz.estore.common.util.FileUtil;
import com.probiz.estore.system.service.SystemConfigManager;
import com.probiz.estoresa.theme.model.Theme;
import com.probiz.estoresa.theme.service.ThemeService;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;

public class ThemeServiceImpl implements ThemeService {

    private SystemConfigManager systemConfigManager=null;

    public void setSystemConfigManager(SystemConfigManager systemConfigManager) {
        this.systemConfigManager = systemConfigManager;
    }

    public void uploadTheme() {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public List<Theme> getThemeList(String themePath) throws Exception {
        File themeDir=new File(themePath);

        File[] themes=themeDir.listFiles();

        List<Theme> themeList=new ArrayList<Theme>();

        String storeFrontUrl= ConfigUtil.getInstance().getStoreFrontSiteUrl();

        for(File themeFile:themes){
           Theme theme=new Theme();

            theme.setThemeName(themeFile.getName());
            theme.setThemePath(themeFile.getPath());
            theme.setSmallScreenshotUrl(storeFrontUrl+"/themes/"+theme.getThemeName()+"/screenshot_s.jpg");
             theme.setScreenshotUrl(storeFrontUrl+"/themes/"+theme.getThemeName()+"/screenshot.jpg");
            File readmeFile=new File(themePath+"/"+theme.getThemeName()+"/readme.properties");
            
            if(!themeFile.isHidden()&& readmeFile.exists()){
            
            
            Properties readmeProperties = new Properties();
            FileInputStream fis=null;
                try {
                	fis=new FileInputStream(readmeFile);	
                    readmeProperties.load(fis);
                    
                    if(readmeProperties.getProperty("author") != null && StringUtils.isNotBlank(readmeProperties.getProperty("author"))){
                    	theme.setAuthor(new String(readmeProperties.getProperty("author").getBytes( "ISO-8859-1"),"UTF-8"));
                    }
                    if(readmeProperties.getProperty("createTime") != null && StringUtils.isNotBlank(readmeProperties.getProperty("createTime"))){
                    	theme.setCreateTime(new String(readmeProperties.getProperty("createTime").getBytes( "ISO-8859-1"),"UTF-8"));
                    }
                    if(readmeProperties.getProperty("description") != null && StringUtils.isNotBlank(readmeProperties.getProperty("description"))){
                    	theme.setDescription(new String(readmeProperties.getProperty("description").getBytes( "ISO-8859-1"),"UTF-8"));
                    }
                    if(readmeProperties.getProperty("version") != null && StringUtils.isNotBlank(readmeProperties.getProperty("version"))){
                    	theme.setVersion(new String(readmeProperties.getProperty("version").getBytes( "ISO-8859-1"),"UTF-8"));        
                    }
                } catch (IOException e) {
                    throw e;
                } finally {
                  if(fis!=null)	
                	 fis.close();
                  readmeProperties.clear();  
                }


            String activeTheme=ConfigUtil.getInstance().getStoreFrontTheme();

            if(activeTheme.equals(theme.getThemeName())){
               theme.setActive(true);
               themeList.add(0,theme);
            }
            else{
                theme.setActive(false); 
              themeList.add(theme);
            }    
          }   
        }


        return themeList;
    }

    public void activeTheme(String themeName) {
//        ConfigUtil.getInstance().getStoreFrontTheme();
        systemConfigManager.saveConfigByKey("StoreFrontTheme",themeName);

        

    }

    public void deleteTheme(String themeName) throws IOException {
        String installPath= ConfigUtil.getInstance().getStoreFrontInstallationPath();
      String themePath=installPath+"/themes/"+themeName;

        FileUtil.deleteDirectory(themePath); 
        //To change body of implemented methods use File | Settings | File Templates.
    }
}
